{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 型について"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 型の確認\n",
    "\n",
    "* typeを使って調べる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "<class 'bytes'>\n",
      "<class 'int'>\n",
      "<class 'float'>\n",
      "<class 'list'>\n",
      "<class 'tuple'>\n",
      "<class 'dict'>\n",
      "<class 'set'>\n"
     ]
    }
   ],
   "source": [
    "### 型の例\n",
    "print(type('Hello'))\n",
    "print(type(b'Hello'))\n",
    "print(type(-123))\n",
    "print(type(12.3))\n",
    "print(type([1,2,3]))\n",
    "print(type((1,2,3)))\n",
    "print(type({\"a\":  1, \"b\": 2}))\n",
    "print(type(set('abc')))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* `<class 'XXX'>`\n",
    "    * Python3においてクラスと型は全く同じ意味を持つ\n",
    "* list型\n",
    "    * Pythonの配列\n",
    "    * シーケンス型\n",
    "        * `[]`を使ってindexで要素にアクセスできる\n",
    "* tuple型\n",
    "    * 複数の要素から構成されそれを一つのモノとして扱える機能\n",
    "    * シーケンス型\n",
    "    * list型との違い\n",
    "        * tupleは作成した後に要素の追加や削除が出来ない\n",
    "* dict型\n",
    "    * Pythonのハッシュ\n",
    "        * キーと値のペアのリスト\n",
    "    * シーケンス型ではない\n",
    "* set型\n",
    "    * 複数のデータを持つデータ型の一つで、集合を表現することができる\n",
    "    * シーケンス型ではない\n",
    "    * set型の特徴\n",
    "        * 重複のないデータ型\n",
    "            * 同じ値は一つのみ登録可能\n",
    "        * in演算子が高速\n",
    "        * 集合に関する関数が豊富\n",
    "* str型\n",
    "    * 文字列\n",
    "    * unicode文字列として扱われる\n",
    "        * Python2の「u'hoge'」と同じ\n",
    "* bytes型\n",
    "    * バイト列\n",
    "    * バイナリデータ\n",
    "        * 文字列っぽく使えるがstr型と連結はできない\n",
    "    * 用途\n",
    "        * 特定の文字コードの文字列を持ちたい場合など\n",
    "\n",
    "※ 型のメソッド等はtype.ipynbを参照"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## list型\n",
    "\n",
    "* Pythonの配列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 'hello', 3.14]\n",
      "[1, 2, 3]\n",
      "[4, 2, 3]\n",
      "1\n",
      "hello\n",
      "3.14\n",
      "1\n",
      "9\n",
      "[3, 4, 5]\n",
      "c[-5:-2]\n",
      "[5, 6, 7]\n",
      "[3, 4, 5, 6, 7, 8, 9]\n",
      "[1, 2, 3, 4, 5]\n",
      "[1, 3, 5, 7]\n"
     ]
    }
   ],
   "source": [
    "### 基本\n",
    "a = [1,'hello',3.14]\n",
    "print(a)\n",
    "\n",
    "### 改行して記述できる\n",
    "b = [\n",
    "    1,\n",
    "    2,\n",
    "    3\n",
    "]\n",
    "print(b)\n",
    "\n",
    "### 要素の値を変更できる\n",
    "b[0] = 4\n",
    "print(b)\n",
    "\n",
    "### 各要素にforでアクセス\n",
    "for x in a:\n",
    "    print(x)\n",
    "\n",
    "### indexでアクセス\n",
    "### この場合index 0は「1」、index 1は「2」\n",
    "c = [1,2,3,4,5,6,7,8,9]\n",
    "print(c[0])\n",
    "\n",
    "### 一番後ろの要素\n",
    "print(c[-1])\n",
    "\n",
    "### c[n:m] でindex nからindex m-1まで。Rubyだとc[n...m]\n",
    "print(c[2:5])\n",
    "\n",
    "### c[-n:-m]で　一番後ろからn個分の要素から一番後ろからm個分の要素を除く\n",
    "print('c[-5:-2]')\n",
    "print(c[-5:-2])\n",
    "\n",
    "### index 2以降の要素\n",
    "print(c[2:])\n",
    "\n",
    "### index 5より前(index 5の要素は含まない)の要素\n",
    "print(c[:5])\n",
    "\n",
    "### c[n:m:l]で　index n から index m-1 まで l個飛ばしで参照\n",
    "print(c[0:8:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 4, 5]"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### Ruby\n",
    "ruby -e \"c = [1,2,3,4,5,6,7,8,9]; print c[2...5]\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### メソッド\n",
    "\n",
    "* len\n",
    "    * リストの要素の個数を調べる\n",
    "* tuple\n",
    "    * リストをタプルに変換する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "(1, 2, 3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4,5]\n",
    "print(len(a))\n",
    "print(tuple(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* in演算子\n",
    "    * 値が要素に存在するか調べる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4,5]\n",
    "print(3 in a)\n",
    "print(9 in a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* map\n",
    "    * リストの各要素に演算をしてくれる関数\n",
    "    * ` map(func, iterable, ...) `\n",
    "        * 第一引数 : \n",
    "        * 第二引数以降\n",
    "            * iterableを順にfuncに渡した結果をyieldで返す \n",
    "            * 第一引数の関数の引数に順番に渡される"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<map object at 0x11372fb00>\n",
      "[1, 2, 3]\n",
      "[1.0, 2.0, 3.0]\n",
      "['1', '2', '3']\n",
      "[2, 4, 6]\n",
      "[2, 4, 6]\n",
      "(2, 4, 6)\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3]\n",
    "b = [7,8,9]\n",
    "\n",
    "### iterator objectが返る\n",
    "print(map(int, a))\n",
    "\n",
    "### 第一引数に型を指定すると、要素の型を変更・指定して返す\n",
    "print(list(map(int, a )))\n",
    "print(list(map(float, a )))\n",
    "print(list(map(str, a )))\n",
    "\n",
    "### 2倍する関数を定義して各要素に演算を適用する\n",
    "def two_times(x):\n",
    "    return x * 2\n",
    "print(list(map(two_times, a )))\n",
    "\n",
    "### ラムダ式を使って1行で演算する\n",
    "print(list(map(lambda x: x*2 , a )))\n",
    "\n",
    "### listではなくtupleではさむとtupleになる\n",
    "print(tuple(map(lambda x: x*2 , a )))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* filter\n",
    "    * リストの要素を抽出して処理する\n",
    "    * `filter(func, iterable)`\n",
    "        * iterableのオブジェクトを順にfuncに渡し、funcがTrueで返したオブジェクトをyieldで返す"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 6, 9]\n",
      "['a', 'b', 'c']\n"
     ]
    }
   ],
   "source": [
    "### 3で割れるものを抽出する\n",
    "a = [1,2,3,4,5,6,7,8,9]\n",
    "print(list(filter(lambda x: x  % 3 == 0, a)))\n",
    "\n",
    "### 空の要素を削除する\n",
    "b = ['a','b','','c','','']\n",
    "print(list(filter(lambda x: x != '', b)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* reduce\n",
    "    * リストの最初の2要素を引数に処理を呼び出し、結果と次の要素を引数に処理の呼び出しを繰り返して単一の結果を返す\n",
    "    * Python3からグローバル空間から取り除かれた\n",
    "        * functoolsをインポートして使う"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "55"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from functools import reduce\n",
    "a = [1,2,3,4,5,6,7,8,9,10]\n",
    "reduce(lambda x ,y : x + y, a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "55"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### Ruby\n",
    "ruby -e \"\n",
    "a = [1,2,3,4,5,6,7,8,9,10]\n",
    "print a.reduce{|x,y| x + y}\n",
    "\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### リスト内包表記"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10, 12, 14, 16, 18]\n",
      "[3, 6, 9]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4,5,6,7,8,9]\n",
    "print([x * 2 for x in a])\n",
    "print([x for x in a if x % 3 == 0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 要素の追加、削除"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* append\n",
    "    * 最後に要素を追加する\n",
    "* insert\n",
    "    * 指定した位置に要素を追加する\n",
    "* del\n",
    "    * 要素を削除する\n",
    "* pop\n",
    "    * 指定したindexの要素を削除する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "['hoge', 1, 2, 3, 4]\n",
      "['hoge', 1, 2, 'insert index 3', 3, 4]\n",
      "[1, 2, 'insert index 3', 3, 4]\n",
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3]\n",
    "\n",
    "a.append(4)\n",
    "print(a)\n",
    "\n",
    "a.insert(0,'hoge')\n",
    "print(a)\n",
    "\n",
    "a.insert(3,'insert index 3')\n",
    "print(a)\n",
    "\n",
    "del a[0]\n",
    "print(a)\n",
    "\n",
    "a.pop(2)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* a[m,n] = [9 ,9]\n",
    "    * index m からindex n-1 を削除して 9,9の要素をindex mから後ろに挿入する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 9, 9, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4,5]\n",
    "a[2:2] = [9,9]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 9, 9, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4,5]\n",
    "a[2:3] = [9,9]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 9, 9, 5]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4,5]\n",
    "a[2:4] = [9,9]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 9, 9]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4,5]\n",
    "a[2:5] = [9,9]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 重複した要素を削除\n",
    "\n",
    "* いったんsetにしてからlistに戻す"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,2,4,5,3,4,5,6,1,2,3,4,1,5]\n",
    "print(list(set(a)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## リストの結合\n",
    "\n",
    "* `+`\n",
    "    * リストを結合したものを表示する\n",
    "* extend\n",
    "    * リストを結合する\n",
    "    * 破壊的メソッド\n",
    "* `*`\n",
    "    * 複数の値の繰り返しのリストを生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3]\n",
    "b = [4,5]\n",
    "\n",
    "print(a + b)\n",
    "a.extend(b)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 1, 2, 3, 1, 2, 3]"
      ]
     },
     "execution_count": 213,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,3]\n",
    "a * 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for文でindexを取得する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hoge\n",
      "fuga\n",
      "haga\n",
      "================================================\n",
      "0 : hoge\n",
      "1 : fuga\n",
      "2 : haga\n",
      "================================================\n",
      "0 : hoge\n",
      "1 : fuga\n",
      "2 : haga\n"
     ]
    }
   ],
   "source": [
    "a = [\"hoge\",\"fuga\",\"haga\"]\n",
    "\n",
    "### 普通のfor文\n",
    "for i in a:\n",
    "    print(i)\n",
    "\n",
    "print('================================================')\n",
    "### rangeを使ってindexを取得する\n",
    "for i in range(len(a)):\n",
    "    item = a[i]\n",
    "    print('{0} : {1}'.format(i, item))\n",
    "    \n",
    "print('================================================')\n",
    "### enumeratorを使ってindexを取得する \n",
    "for i, j in enumerate(a):\n",
    "    print('{0} : {1}'.format(i, j))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## tuple型\n",
    "\n",
    "* 複数の要素から構成されそれを一つのモノとして扱える機能\n",
    "* list型との違い\n",
    "    * tupleは作成した後に要素の追加や削除が出来ない"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 'hello', 3.14)\n",
      "(1, 2, 3)\n",
      "タプルの要素は変更できません\n",
      "10\n",
      "(10,)\n"
     ]
    }
   ],
   "source": [
    "### 基本\n",
    "a = (1,'hello',3.14)\n",
    "print(a)\n",
    "\n",
    "### 改行して記述できる\n",
    "b = (\n",
    "    1,\n",
    "    2,\n",
    "    3\n",
    ")\n",
    "print(b)\n",
    "\n",
    "### 要素の変更はできない\n",
    "try:\n",
    "    b[0] = 4\n",
    "except TypeError:\n",
    "    print(u'タプルの要素は変更できません')\n",
    "\n",
    "### 要素が一つのtupleを作る場合は最後にカンマを入れる\n",
    "### 単なる数値の10\n",
    "c = (10)\n",
    "print(c)\n",
    "\n",
    "### index 0の要素10のみのタプル\n",
    "d = (10,)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### メソッド\n",
    "\n",
    "* len\n",
    "    * タプルの要素の個数を調べる\n",
    "* list\n",
    "    * タプルをリストに変換する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "a = (1,2,3,4,5)\n",
    "print(len(a))\n",
    "print(list(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## dict型\n",
    "\n",
    "* Pythonのハッシュ\n",
    "    * キーと値のペアのリスト"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 'Hello', 'b': 123, 'c': 4.56, 'd': [1, 2, 3], 'e': (4, 5, 6), 'f': {'y', 'z', 'x'}}\n",
      "123\n"
     ]
    }
   ],
   "source": [
    "a = {\n",
    "    'a': 'Hello',\n",
    "    'b': 123,\n",
    "    'c': 4.56,\n",
    "    'd': [1,2,3],\n",
    "    'e': (4,5,6),\n",
    "    'f': set('xyz')\n",
    "}\n",
    "print(a)\n",
    "\n",
    "### keyからvalueにアクセス\n",
    "print(a['b'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'def': 'ghi'}\n",
      "{'def': 'ghi', 'jkl': 'mnl'}\n"
     ]
    }
   ],
   "source": [
    "abc = {}\n",
    "abc['def'] = 'ghi'\n",
    "print(abc)\n",
    "\n",
    "abc['jkl'] = 'mnl'\n",
    "print(abc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### メソッド"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', 'Hello')\n",
      "('b', 123)\n",
      "('d', [1, 2, 3])\n",
      "key = a, value = Hello\n",
      "key = b, value = 123\n",
      "key = d, value = [1, 2, 3]\n",
      "key = a\n",
      "key = b\n",
      "key = d\n",
      "value = Hello\n",
      "value = 123\n",
      "value = [1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "a = {\n",
    "    'a': 'Hello',\n",
    "    'b': 123,\n",
    "    'd': [1,2,3]\n",
    "}\n",
    "\n",
    "### tupleでkeyと値のセットを返す\n",
    "for x in a.items():\n",
    "    print(x)\n",
    "\n",
    "### 引数を２つにするとkeyと値をそれぞれ返す\n",
    "for key, value in a.items():\n",
    "    print('key = {0}, value = {1}'.format(key, value))\n",
    "\n",
    "### keyを返す\n",
    "for key in a.keys():\n",
    "    print('key = {0}'.format(key))\n",
    "\n",
    "### valueを返す\n",
    "for value in a.values():\n",
    "    print('value = {0}'.format(value))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* dictの中のlistにdictを追加する\n",
    "    * 参考 https://blogs.yahoo.co.jp/dpdtp652/39381397.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'member': [{'age': 38, 'name': 'Takao'}]}\n",
      "{'member': [{'age': 38, 'name': 'Takao'}, {'age': 5, 'name': 'Sosuke'}]}\n"
     ]
    }
   ],
   "source": [
    "faminfo = {'member': [{'name': 'Takao', 'age': 38 }] }\n",
    "print(faminfo)\n",
    "\n",
    "Sosuke = {'name': 'Sosuke', 'age': 5 }\n",
    "\n",
    "faminfo[\"member\"].append(Sosuke)\n",
    "print(faminfo)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## set型\n",
    "\n",
    "* 複数のデータを持つデータ型の一つで、集合を表現することができる\n",
    "* シーケンス型ではない\n",
    "* set型の特徴\n",
    "    * 重複のないデータ型\n",
    "        * 同じ値は一つのみ登録可能\n",
    "    * in演算子が高速\n",
    "    * 集合に関する関数が豊富\n",
    "    \n",
    "### 参考\n",
    "\n",
    "* [セット（Set）に入門する](http://www.yoheim.net/blog.php?q=20160605)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'x', 'y', 'z'}"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set('xyz')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## str型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "テスト\n",
      "テスト\n",
      "1234\n",
      "5678\n"
     ]
    }
   ],
   "source": [
    "print('テスト')\n",
    "\n",
    "### u'～'と同じ\n",
    "print(u'テスト')\n",
    "\n",
    "### 改行を含む\n",
    "print('1234\\n5678')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## メソッド"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 文字列をリストに入れる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['あ', 'い', 'う', 'え', 'お', ' ', 'か', 'き', 'く', 'け', 'こ']\n",
      "['あいうえお', 'かきくけこ']\n",
      "あいうえお\n",
      "かきくけこ\n",
      "['あいうえお', 'かきくけこ']\n"
     ]
    }
   ],
   "source": [
    "### デフォルトは1文字ずつ要素に入る\n",
    "a = 'あいうえお かきくけこ'\n",
    "print(list(a))\n",
    "\n",
    "### 半角空白を区切りに指定\n",
    "b = 'あいうえお かきくけこ'.split(\" \")\n",
    "print(list(b))\n",
    "\n",
    "### 改行を区切りに指定\n",
    "c = 'あいうえお\\nかきくけこ'\n",
    "print(c)\n",
    "print(c.split(\"\\n\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## bytes型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\xe3\\x83\\x86\\xe3\\x82\\xb9\\xe3\\x83\\x88utf-8'\n",
      "b'\\x83e\\x83X\\x83gshift-jis'\n",
      "b'\\xa5\\xc6\\xa5\\xb9\\xa5\\xc8euc-jp'\n",
      "================================================\n",
      "テストutf-8\n",
      "テストshift-jis\n",
      "テストeuc-jp\n"
     ]
    }
   ],
   "source": [
    "utf8 = 'テストutf-8'.encode('utf-8')\n",
    "shiftjis = 'テストshift-jis'.encode('shift-jis')\n",
    "eucjp = 'テストeuc-jp'.encode('euc-jp')\n",
    "\n",
    "print(utf8)\n",
    "print(shiftjis)\n",
    "print(eucjp)\n",
    "print('================================================')\n",
    "print(utf8.decode('utf-8'))\n",
    "print(shiftjis.decode('shift-jis'))\n",
    "print(eucjp.decode('euc-jp'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
